import { defineStore } from 'pinia'
import { ref } from 'vue'
import defaultSettings from '@/settings'
// setup
export const useAppStore = defineStore(
  'app',
  () => {
    // state
    const device = ref<string>('default')
    const language = ref<string>(defaultSettings.language)
    const theme = ref<string>(defaultSettings.theme)
    // actions
    function toggleDevice (val: string) {
      device.value = val
    }
    function getData (val: string) {
      return new Promise<String>((resolve, reject) => {
        try {
          setTimeout(() => {
            resolve(val)
          }, 2000)
        } catch {
          reject()
        }
      })
    }
    return {
      device,
      language,
      theme,
      toggleDevice,
      getData
    }
  },
  {
    persist: {
      enabled: true, // 开启
      strategies: [
        {
          key: 'app', // 存储的key
          storage: localStorage, // 可以选择对应的存储形式（localStorage或者sessionStorage）
          paths: ['device'] // 存储字段名
        },
        {
          key: 'app2',
          storage: sessionStorage,
          paths: ['language', 'theme']
        }
      ]
    }
  }
)
